* =======================================================================================================================================================
*
* Code Description: 
* This codefile computes the figures provided in Table 5: Direct evidence on cross-fund contagion.
*
* =======================================================================================================================================================
*
* Major output:
* Table 5: Direct evidence on cross-fund contagion.
* =======================================================================================================================================================
*
* General disclaimer:
* This file directory produces replication code for "Connected Funds". 
* Because we cannot share the underlying data provided by the Bundesbank's Research Data and Service Centre (RDSC) and other subscription data sources, 
* we have included pseudo data to show how the raw data are formatted. 
* Other researchers can go through a similar approval and subscription process to obtain the underlying data. (2023-04-06)
*
* =======================================================================================================================================================


// Paths
global Path "C:\ConnectedFunds_Codebase\"
sysdir set STBPLUS "${Path}Code\Ado"   

clear all
est clear
set more off 

set scheme plottig, perm


* ============================================================= Preparations ============================================================================

// Construct dataset
import delimited "${Path}Data\FalatoEtAl\FalatoEtAl.txt"

merge 1:1 isin datum using "${Path}Data\JiangEtAl\FullData.dta", keepusing(lag_holdings_FundShares)
keep if _merge ==3 
drop _merge

// Condition on funds with fund holdings > 0
keep if lag_holdings_FundShares > 0
drop if lag_holdings_FundShares == . 

// Generate time and panel variables 
egen ui_fund = group(isin)
egen ui_date = group(datum)

// Set-up panel
sort ui_fund ui_date
xtset ui_fund ui_date

gen retail = 0 
replace retail = 1 if ucitsaif <200

// Generate standardized x-vars
foreach x of varlist peerflowpressure_bond peerflowpressure_sales_bond peerflowpressure_buys_bond peerflowpressure_equity peerflowpressure_sales_equity peerflowpressure_buys_equity peerflowpressure_fund  peerflowpressure_sales_fund peerflowpressure_buys_fund peerflowpressure_fund1 peerflowpressure_sales_fund1 peerflowpressure_buys_fund1{

	winsor2 `x', suffix(W) cuts(1 99) by(artmittel retail)
	egen sd_`x' = std(`x'W)

}

// Generate additional control variables
gen lag_tna = L.fondsverm
gen lag_log_tna = log(lag_tna)
gen log_family_tna = log(kag_tna)
gen lag_log_family_tna = L.log_family_tna

// Generate and winsorize y-vars
egen tmp = wtmean(netflows), by(artmittel ui_date retail)  weight(lag_tna)
gen pct_netflows = 100*netflows
drop tmp

egen tmp = wtmean(returndiv), by(artmittel ui_date retail) weight(lag_tna)
gen pct_returndiv = 100*(returndiv - tmp)
drop tmp

foreach x of varlist pct_returndiv pct_netflows{

	winsor2 `x', suffix(W) cuts(1 99) by(artmittel retail)
	egen sd_`x' = std(`x'W)

}


* ================================================= Run regressions a la falato et al ==================================================================


global maincontrols_Fund "sd_peerflowpressure_bond sd_peerflowpressure_equity sd_peerflowpressure_fund" 
global maincontrols_Fund_BuysSales "sd_peerflowpressure_buys_bond sd_peerflowpressure_sales_bond sd_peerflowpressure_buys_equity sd_peerflowpressure_sales_equity sd_peerflowpressure_buys_fund sd_peerflowpressure_sales_fund" 
global additionalcontrols_Fund "lag_log_tna lag_log_family_tna L.pct_netflows L.pct_returndiv" 

label define artmittel_lbl 1 "EquityFund" 2 "BondFund" 3 "MixedFund"  7 "FundOfFund"
label values artmittel artmittel_lbl
label define retail_lbl 0 "Institutional" 1 "Retail"
label values retail retail_lbl

foreach x of varlist pct_returndivW pct_netflowsW {

	if "`x'" == "pct_returndivW" {
	global text "Return"
	}
	if "`x'" == "pct_netflowsW" {
	global text "Flows"
	}

	gen y = `x'
	label variable y ${text}
	
	// Run regressions
	
	// Panel A: baseline
	reghdfe y ${maincontrols_Fund} ${additionalcontrols_Fund}, absorb(ui_fund ui_date) cluster(ui_fund ui_date)
	est store baseb_1, title("All")

	reghdfe y ${maincontrols_Fund}  ${additionalcontrols_Fund} if retail == 1, absorb(ui_fund ui_date) cluster(ui_fund ui_date)
	est store baseb_2, title("Retail")

	reghdfe y ${maincontrols_Fund}  ${additionalcontrols_Fund} if retail == 0, absorb(ui_fund ui_date) cluster(ui_fund ui_date)
	est store baseb_3, title("Institutional")
	
	// Panel B: buys and sales separately
	reghdfe y ${maincontrols_Fund_BuysSales} ${additionalcontrols_Fund}, absorb(ui_fund ui_date) cluster(ui_fund ui_date)
	est store buysaleb_1, title("All")

	reghdfe y ${maincontrols_Fund_BuysSales}  ${additionalcontrols_Fund} if retail == 1, absorb(ui_fund ui_date) cluster(ui_fund ui_date)
	est store buysaleb_2, title("Retail")

	reghdfe y ${maincontrols_Fund_BuysSales}  ${additionalcontrols_Fund} if retail == 0, absorb(ui_fund ui_date) cluster(ui_fund ui_date)
	est store buysaleb_3, title("Institutional")

	drop y
	
	// Export results
	
	// Panel A: baseline
	esttab baseb_1 baseb_2 baseb_3 using "${Path}Data\temp.csv",  /// 
	b(3) t(3) star(* 0.10 ** 0.05 *** 0.01) /// 
	stats(r2 r2_a r2_within r2_a_within N absvars clustvar lincom_b lincom_se lincom_tval, /// 
	fmt(%9.4g %9.4g  %9.4g %9.4g %16.0g %16.0g %9.4g %9.4g) ///
	) nonotes noomitted mtitles label replace 

	preserve
	clear
	insheet using "${Path}Data\temp_Falato.csv"
	export excel using "${Path}Paper\Tables\Tab5_Direct evidence on cross-fund contagion.xlsx", sheet(${text})  sheetmodify   cell(B5)
	restore
	
	// Panel B: buys and sales separately
	esttab buysaleb_1 buysaleb_2 buysaleb_3 using "${Path}Data\temp_Falato.csv",  /// 
	b(3) t(3) star(* 0.10 ** 0.05 *** 0.01) /// 
	stats(r2 r2_a r2_within r2_a_within N absvars clustvar lincom_b lincom_se lincom_tval, /// 
	fmt(%9.4g %9.4g  %9.4g %9.4g %16.0g %16.0g %9.4g %9.4g) ///
	) nonotes noomitted mtitles label replace 

	preserve
	clear
	insheet using "${Path}Data\temp_Falato.csv"
	export excel using "${Path}Paper\Tables\Tab5_Direct evidence on cross-fund contagion.xlsx", sheet(${text}-BuysSales)  sheetmodify   cell(B5)
	restore

	est clear
}

// Do some housekeeping
erase "${Path}Data\temp_Falato.csv"